<?php
$cache_time=10;
$OJ_CACHE_SHARE=false;
require_once("config/path_config.php");
require_once('./include/cache_start.php');
require_once('./include/db_info.inc.php');
require_once('./include/setlang.php');
require_once("./include/const.inc.php");
require_once("./config/privliege_config.php");

function getParam()
{
    if( !isset($_GET['id']) ) {
        return null;
    }

    $id=intval($_GET['id']);

    $page=0;
    if (isset( $_GET['page']) ) {
        $page=strval(intval($_GET['page']));
    }

    return array("id" => $id, "page" => $page);
}

// 获取总共提交次数
function getTotalSolution( $problemId )
{
    $sql="SELECT count(*) FROM solution WHERE problem_id=?";
    $result=pdo_query($sql, $problemId);
    $row=$result[0];
    if ( ! isset($row[0]) ) {
        return null;
    }
    $total=intval($row[0]);
    return $total;
}

// 获取题目提交的总人数
function getTotalSubmitUser($problemId, $isAc = 0 )
{
    $sql="SELECT count(DISTINCT user_id) FROM solution WHERE problem_id=?";
    if ( $isAc ) {
        $sql .= " AND result=4";
    }

    $result=pdo_query( $sql, $problemId);
    $row=$result[0];
    if ( ! isset($row[0]) ) {
        return null;
    }
    $total=intval($row[0]);
    return $total;
}

function getProblemInfo($problemId)
{
    $sql="SELECT `title` FROM problem WHERE problem_id=?";
    $result=pdo_query( $sql, $problemId);
    $row=$result[0];
    if ( ! isset($row[0]) ) {
        return null;
    }
    return array("id" => $problemId, "title" => $row[0]);
}

// 判断用户是否已登录
if (!isset($_SESSION['user_id'])) {
    $view_errors= "<a href=loginpage.php>$MSG_Login</a>";
    require("template/bs3/error.php");
    exit(0);
}

$param = getParam();
if ( $param == null ) {
    $view_errors= "参数错误";
    require("template/bs3/error.php");
    exit(0);
}

$problemInfo = getProblemInfo($param['id']);

$totalSubmitCnt = getTotalSolution($param['id']);
if ( $totalSubmitCnt == null ) {
    $view_errors = "未查询到该题目的提交信息";
    $displayRet = true;
    require("template/bs3/error.php");
    exit(0);
}

$submitUserCnt = getTotalSubmitUser($param['id']);
$acuserCnt = getTotalSubmitUser($param['id'], 1);

// 界面展示内容
$problemId = $param['id'];
$view_problem=array();
$view_problem[0][0]="提交总次数";
$view_problem[0][1]=$totalSubmitCnt;

$view_problem[1][0]="提交总人数";
$view_problem[1][1]=$submitUserCnt;

$view_problem[2][0]="正确总人数";
$view_problem[2][1]=$acuserCnt;

$i=3;
$sql="SELECT result,count(1) FROM solution WHERE problem_id=? AND result>=4 group by result order by result";
$result=pdo_query( $sql, $param['id']);
foreach($result as $row){
    $view_problem[$i][0] =$jresult[$row[0]];
    $view_problem[$i][1] ="<a href=status.php?problem_id=$problemId&jresult=".$row[0]." >".$row[1]."</a>";
    $i++;
}

// AC用户分页
$page = $param['page'];
$pagemin = 0;
$pagemax = intval(($acuserCnt-1)/20);
if ( $page<$pagemin ) {
    $page=$pagemin;
}
if ( $page>$pagemax ) {
    $page=$pagemax;
}
$start=$page*20;
$sz=20;
if ( $start+$sz > $acuserCnt ) {
    $sz=$acuserCnt-$start;
}


// check whether the problem in a contest
$now=strftime("%Y-%m-%d %H:%M",time());
$sql="SELECT 1 FROM `contest_problem` WHERE `problem_id`=$problemId AND `contest_id` IN (
        SELECT `contest_id` FROM `contest` WHERE `start_time`<? AND `end_time`>?)";
$rrs=pdo_query($sql,$now,$now);
$flag=count($rrs)==0;

// check whether the problem is ACed by user
$AC=false;
if (isset($OJ_AUTO_SHARE)&&$OJ_AUTO_SHARE&&isset($_SESSION['user_id'])){
        $sql="SELECT 1 FROM solution where
                        result=4 and problem_id=? and user_id=?";
        $rrs=pdo_query( $sql, $problemId, $_SESSION['user_id']);
        $AC=(intval(count($rrs))>0);
        
}

/*
$sql="SELECT c.*, b.*, d.user_type FROM (
  SELECT COUNT(*) att, user_id, min(10000000000000000000 + time*100000000000 + memory*100000 + code_length) score
  FROM solution
  WHERE problem_id =? AND result =4
  GROUP BY user_id
  ORDER BY in_date ASC
)c
LEFT JOIN (
  SELECT solution_id, user_id, user_name, `language`, 10000000000000000000 + time*100000000000 + memory*100000 + code_length score, in_date
  FROM solution 
  WHERE problem_id =? AND result =4  
  ORDER BY score, in_date DESC
)b ON b.user_id=c.user_id AND b.score=c.score
LEFT JOIN users d on d.user_id = b.user_id where user_type=1
ORDER BY in_date DESC
LIMIT  $start, $sz;";
*/
$sql="SELECT c.*, b.*, d.user_type FROM (
  SELECT COUNT(*) att, user_id, code_length, min(10000000000000000000 + time*100000000000 + memory*100000) score 
  FROM solution
  WHERE problem_id =? AND result =4
  GROUP BY user_id
  ORDER BY in_date ASC
)c
LEFT JOIN (
  SELECT solution_id, user_id, user_name, `language`, code_length, 10000000000000000000 + time*100000000000 + memory*100000 score, in_date
  FROM solution 
  WHERE problem_id =? AND result =4  
  ORDER BY score, in_date DESC
)b ON b.user_id=c.user_id AND b.score=c.score
LEFT JOIN users d on d.user_id = b.user_id where user_type=1
ORDER BY score asc
LIMIT  $start, $sz;";

$result=pdo_query( "SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''))");
$result=pdo_query( $sql, $problemId, $problemId);

$view_solution=array();
$j=0;
$last_user_id='';
$i=$start+1;
foreach($result as $row){
    if( $row['user_id'] == $last_user_id ) {
        continue;
    }

    $sscore=strval($row['score']);
    $s_time=intval(substr($sscore,1,8));
    $s_memory=intval(substr($sscore,9,6));
    $s_cl=intval($row['code_length']);
    //$s_cl=intval(substr($sscore,15,5));

    $view_solution[$j][0]= $i;
    $i ++;
    $view_solution[$j][1]= $row['solution_id'];
    if ( intval($row['att']) > 1 ) {
        $view_solution[$j][1].=  "(".$row['att'].")";
    }
    $view_solution[$j][2]=  "<a href='userinfo.php?user=".$row['user_id']."'>".$row['user_id']."</a>";
    $view_solution[$j][3]= $row['user_name'];
    if ($flag) {
        $view_solution[$j][4]=  "$s_memory KB";
        $view_solution[$j][5]=  "$s_time ms";
    }
    else {
        $view_solution[$j][4]=  "----";
        $view_solution[$j][5]=  "----";
    }

    // 1. 用户与当前登陆的用户相同
    $allowSeeCode = isset($_SESSION['user_id']) && strcasecmp($row['user_id'],$_SESSION['user_id']) == 0;
    // 2. 用户是管理员
    $allowSeeCode |= isset($_SESSION['administrator']);
    // 3. 用户是教师
    $allowSeeCode |= isset($_SESSION['teacher']);

    // 系统允许相互之间查看代码
    $allowSeeCode |= isset($_SESSION['user_id']) && PrivliegeConfig::$IS_ALLOW_SEE_CODE;
    if ( $allowSeeCode ) {
        $view_solution[$j][6]=  "<a target=_blank href=showsource.php?id=".$row['solution_id']
            .">".$language_name[$row['language']]."</a>";
    }
    else {
        $view_solution[$j][6]= $language_name[$row['language']];
    }

    // 代码长度
    if ($flag) {
        $view_solution[$j][7]=  "$s_cl B";
    }
    else {
        $view_solution[$j][7]=  "----";
    }
    $view_solution[$j][8]=  $row['in_date'];
    $j++;
    $last_user_id=$row['user_id'];
}


$view_recommand=Array();
if(isset($_GET['id'])){
  $id=intval($_GET['id']);
        if(isset($_SESSION['user_id']))$user_id=($_SESSION['user_id']);
	$sql="select source from problem where problem_id=?";
	$result=pdo_query($sql,$id);
	$source=$result[0][0];
        $sql="select problem_id from problem where source like ? and problem_id!=? limit 10";

        $result=pdo_query( $sql,"%$source%",$id);
        $i=0;
         foreach($result as $row){
                $view_recommand[$i][0]=$row['problem_id'];
                $i++;
        }
}

/////////////////////////Template
require("template/bs3/problemstatus.php");
/////////////////////////Common foot
if(file_exists('./include/cache_end.php'))
        require_once('./include/cache_end.php');
?>

